import 'package:flutter/material.dart';

import 'package:decimal/decimal.dart';

class ProductBlock extends StatefulWidget {
  String name;
  String id;
  Image image;

  double price;

  ProductBlock({

    super.key,
    required this.name,
    required this.id,
    required this.image,
    required this.price,
  });

  @override
  State<ProductBlock> createState() => _ProductBlockState();
}

class _ProductBlockState extends State<ProductBlock> {

  @override
  Widget build(BuildContext context) {
    return tapUI(buildUI(), () {
      print(widget.id);
      print(widget.name);
    } );
  }

  Widget tapUI (Widget ui, Function function) {
    return InkWell(child: ui, onTap: () {
      function();
    },);
  }

  Widget buildUI() {
    Size screenSize = MediaQuery.of(context).size;
    double screenWidth = screenSize.width;
    double screenHeight = screenSize.height;

    double cardWidth = screenWidth;
    double cardHeight = screenHeight*(1/4);
    
    TextStyle nameStyle = TextStyle(
        fontSize: 14, // 调整字体大小
        overflow: TextOverflow.ellipsis
    );
    
    TextStyle priceStyle = TextStyle(
      fontSize: 18, // 调整字体大小
      fontWeight: FontWeight.bold,
      color: Colors.red,
    );

    TextStyle priceStyleSmall = TextStyle(
      fontSize: 17, // 调整字体大小
      color: Colors.red,
    );

    TextStyle varStyle = TextStyle(
      fontSize: 12, // 调整字体大小
      color: Colors.grey,
        overflow: TextOverflow.ellipsis
    );



    return Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          //左 图片
          Container(
            alignment: Alignment.topLeft,
            padding: EdgeInsets.fromLTRB(12, 6, 22, 32),
            height: cardHeight,
            width: cardWidth*(1/3)-10,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(12),
              child: widget.image,
            ),
          ),

          // 右 文字
          Container(
            padding: EdgeInsets.fromLTRB(0, 3, 0, 0),
            width: cardWidth*(1- 1/3),

            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
              //上 标题
              Text(
                widget.name,
                style: nameStyle,
              ),

              // 中 参数表
              Container(
                // height: cardHeight * (1/2),
                padding: EdgeInsets.fromLTRB(cardWidth/16, 0, 0, 0),
                child: Row(
                  children: [
                    Padding(padding: EdgeInsets.fromLTRB(12, 7, 12, 8), child: Column(children: [
                      Text("11英寸", style: TextStyle(fontSize: 12,),),
                      Text("11英寸", style: TextStyle(fontSize: 12,),),
                    ],),),
                    Container(color: Colors.grey, width: 1, height: 12,),
                    Padding(padding: EdgeInsets.fromLTRB(12, 7, 12, 8), child: Column(children: [
                      Text("11英寸", style: TextStyle(fontSize: 12,),),
                      Text("11英寸", style: TextStyle(fontSize: 12,),),
                    ],),),
                  ],
                )
              ),

              //下 价格
              Container(

                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    //
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text("￥"+widget.price.toInt().toString(), style: priceStyle,),
                        Text(
                          "." +
                                  () {
                                Decimal decimal = (Decimal.parse(widget.price.toString())  % Decimal.parse("1") * Decimal.parse("100"));
                                int i = decimal.toDouble().toInt();
                                if (i > 9) {
                                  return i.toString();
                                } else {
                                  return "0${i}";
                                }
                              } (),

                          style: priceStyleSmall,
                        ),

                      ],
                    ),
                    Text(
                      "领优惠券",
                      style: varStyle,
                    ),
                    Text(
                      "1万+件售出"+"  "+"98%好评",
                      style: varStyle,
                    ),
                    Row(children: [
                      Expanded(child: Text("XXXXXxxxxxxxxxxxxxxxxxxxXXXXXXXXXX", style: varStyle,),),
                      // Text("  进店> ", style: varStyle,),
                      GestureDetector(
                        onTap: () {
                          print("进店>");
                        },
                        child: TextButton(
                          onPressed: null, // 或者设置 onPressed 为您需要的逻辑
                          child: Text(
                            "进店> ",
                            style: varStyle,
                          ),
                        ),
                      )

                    ],)
                  ],
                )
              ),
            ],)
          ),
        ],
      );
  }

}
